package com.xique.park.command.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.park.api.IParkIpcCommandApi;
import com.xique.park.bean.domain.ParkDeviceSub;
import com.xique.park.command.handler.base.BaseCommand;
import com.xique.park.constant.CommandConstant;
import com.xique.park.constant.ParkApiName;
import com.xique.park.service.service.IParkDeviceSubService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 设置设备参数
 *
 * @author WLD
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class SetDevParamCommandHandler extends BaseCommand {

    private final IParkDeviceSubService parkDeviceSubService;

    private final IParkIpcCommandApi parkIpcCommandApi;

    @Override
    public String getServiceName() {
        return ParkApiName.COMMAND_SET_DEV_PARAM;
    }

    @Override
    public String getVersion() {
        return ParkApiName.VER_3_0;
    }

    @Override
    public AjaxResult issue(Long parkId, Long laneId, String mngTypeIds, String sn,
                            MultipartFile file, String fileName, String cmd) {
        checkParkId(parkId);
        checkLaneId(laneId);
        checkSn(sn);

        JSONObject baseObj = getBaseObj(sn, getServiceName(), getVersion());

        ParkDeviceSub parkDeviceSub = new ParkDeviceSub();
        parkDeviceSub.setParkId(parkId);
        parkDeviceSub.setLaneId(laneId);
        parkDeviceSub.setStatus("normal");
        List<ParkDeviceSub> parkDeviceSubList = parkDeviceSubService.selectParkDeviceSubList(parkDeviceSub);

        if (CollUtil.isNotEmpty(parkDeviceSubList)) {
            JSONObject dtObj = new JSONObject();
            JSONObject scannerObj = new JSONObject();
            JSONObject remainDisplayObj = new JSONObject();
            JSONArray ids = new JSONArray(parkDeviceSubList.size());

            for (ParkDeviceSub deviceSub : parkDeviceSubList) {
                ids.add(deviceSub.getId());
                if ("scanner".equals(deviceSub.getType())) {
                    // id不用下发给设备，只用于后端记录下发结果
                    scannerObj.put("ip", deviceSub.getIp());
                    scannerObj.put("port", StrUtil.isNotEmpty(deviceSub.getPort()) ? Integer.parseInt(deviceSub.getPort()) : null);
                    dtObj.put("scanner", scannerObj);
                } else if ("remain_display".equals(deviceSub.getType())) {
                    remainDisplayObj.put("ip", deviceSub.getIp());
                    remainDisplayObj.put("port", StrUtil.isNotEmpty(deviceSub.getPort()) ? Integer.parseInt(deviceSub.getPort()) : null);
                    dtObj.put("remain_display", remainDisplayObj);
                }
            }
            JSONObject dataObj = new JSONObject();
            dataObj.put(CommandConstant.IDS, ids);
            dataObj.put(CommandConstant.DT, dtObj);
            baseObj.put(CommandConstant.DATA, dataObj);

            @SuppressWarnings("unchecked")
            Map<String, Object> params = JSON.toJavaObject(baseObj, Map.class);
            AjaxResult issueResult = parkIpcCommandApi.issue(params);

            log.info("[停车系统]设置设备参数命令下发，服务名：{}，版本号：{}，车场ID：{}，通道ID：{}，设备编号：{}，下发参数：{}，下发结果：{}",
                    getServiceName(), getVersion(), parkId, laneId, sn, params, issueResult);

            updateCommonInfo(cmd, params, issueResult);

            return issueResult;
        }
        return AjaxResult.success();
    }

}
